package erwiki.ui.app.admin.handlers.workaround2;

import java.util.List;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.swt.widgets.Shell;
//import org.elwiki.admin.app.internal.bundle.AdminAppUiActivator;
//import org.elwiki.api.IWikiEngine;
import org.elwiki_data.WikiPage;

import erwiki.api.core.Engine;
import erwiki.api.pages.PageManager;
import erwiki.api.providers.PageProvider;
import erwiki.ui.app.admin.dialogs.hierachychange.HierarchSelectionyDialog;
import jakarta.inject.Inject;

import org.eclipse.jface.window.Window;

public class ChangePageHierarchy {

	@Inject
	Engine engine;

	@Execute
	public void execute(Shell shell) {
		HierarchSelectionyDialog hierarchyDialog = new HierarchSelectionyDialog(null);

		if (hierarchyDialog.open() == Window.OK) {
			// :FVK: MessageDialog.openInformation(this.window.getShell(), "Информация", "Нажат OK");
			WikiPage masterPage = hierarchyDialog.getMasterPage();
			List<WikiPage> listChildrenPages = hierarchyDialog.getChildrenPages();

			PageManager pm = engine.getManager(PageManager.class);
			PageProvider pp = pm.getCurrentProvider();
			pp.changeHierarchy(masterPage, listChildrenPages);
		}

		/*
		IWikiEngine engine = ElWikiCorePlugin.getDefault().getAppSession().getWikiEngine();
		WikiPage main = engine.getPage("Main");
		WikiPage about = engine.getPage("About");
		
		// // // OK CODE.
		{
			// Записать данные (выполнить транзакцию).
			CDOTransaction transaction = IModel.INSTANCE.getTransactionCDO();
			main = transaction.getObject(main);
			String[] oldParents = new String[] { "p1", "p2" };
			main.setOldParents(oldParents);
			about = transaction.getObject(about);
			main.getChildren().add(about);
			try {
				transaction.commit();
			} catch (CommitException ex) {
				ex.printStackTrace();
			} finally {
				transaction.close();
			}
		}
		*/

		/*
		// Записать данные (выполнить транзакцию).
		CDOTransaction transaction = IModel.INSTANCE.getTransactionCDO();
		main = transaction.getObject(main);
		String[] oldParents = new String[] { "p1", "p2" };
		main.setOldParents(oldParents);
		try {
			transaction.commit();
		} catch (CommitException ex) {
			ex.printStackTrace();
		} finally {
			transaction.close();
		}
		*/
	}

}
